<template>
	<view class="printout">
		<view class="imgBox">
			<image v-if="yearoldImg.length" v-for="(item,index) in yearoldImg" class="imgitem" :src="item" style="width: 216rpx;height: 216rpx;" />
			<image v-for="(item,index) in imgs" class="imgitem" :src="item" style="width: 216rpx;height: 216rpx;" />
			<image class="imgitem" src="@/static/image/shangchuan.png" style="width: 216rpx;height: 216rpx;"
				@click="handleUploadzhaopian" />
		</view>
		<view class="bottomContent">
			<view class="tijiao" @click="handlefeedback">
				完成
			</view>
		</view>
	</view>
</template>

<script>
	import {
		feedback,
		getPhotos
	} from "@/api/api.js"
	export default {
		data() {
			return {
				yearoldImg:[],
				imgs: [],
			}
		},
		onLoad() {
			this.handleGetPhotos()
		},
		methods: {
			async handleGetPhotos() {
				let res = await getPhotos()
				console.log("晒单历史", res)
				this.yearoldImg = res.data
			},
			handleUploadzhaopian() {
				let that = this
				uni.chooseImage({
					count: 9, //默认9
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						console.log("tempFilePaths", tempFilePaths)
						for (let i = 0; i < tempFilePaths.length; i++) {
							uni.uploadFile({
								url: 'https://www.dayige.cc/api/index/upload', //仅为示例，非真实的接口地址
								filePath: tempFilePaths[i],
								name: 'file',
								// formData: {
								// 	'user': 'test'
								// },
								header: {
									"Content-Type": "application/form-data",
									"Authorization": 'Bearer' + ' ' + uni.getStorageSync("token")
								},
								success: (uploadFileRes) => {
									// console.log("图片路径//////", uploadFileRes);
									// console.log("图片路径//////", JSON.parse(uploadFileRes.data).data);
									if (that.imgs.length >= 9) {
										uni.showToast({
											title: '最多可上传9张图片！',
											duration: 1000,
											icon: "none",
										});
									} else {
										that.imgs.push(JSON.parse(uploadFileRes.data).data)
									}
								},
								fail(e) {
									console.log("上传图片", e)
								}
							});
						}
					}
				});


			},

			async handlefeedback() {
				let urls = this.imgs.join(",")
				// console.log("urls",this.imgs,urls)
				// return
				let res = await feedback({
					type: 2,
					url: urls
				})
				if (res.code === 1000) {
					uni.showToast({
						title: '晒单成功！',
						duration: 1000,
						icon: "none",
					});
					uni.setStorageSync("feedbackLength", this.imgs.length)
					uni.navigateTo({
						url: "./index"
					})
				} else {
					uni.showToast({
						title: res.message,
						duration: 1000,
						icon: "none",
					});
				}
			}

		}
	}
</script>

<style scoped lang="scss">
	.printout {
		.imgBox {
			width: 100%;
			padding: 30rpx;
			box-sizing: border-box;
			display: flex;
			flex-wrap: wrap;

			.imgitem {
				margin-right: 20rpx;
			}

			.imgitem:nth-child(3n) {
				margin-right: 0rpx;
			}
		}

		.bottomContent {
			width: 100%;
			position: fixed;
			bottom: 0;
			/* 兼容 iOS 设备 */
			padding-bottom: env(safe-area-inset-bottom);

			.tijiao {
				width: 686rpx;
				height: 132rpx;
				background: linear-gradient(180deg, #FE5C5C 0%, rgba(255, 78, 78, 0.67) 100%);
				border-radius: 60rpx 60rpx 60rpx 60rpx;
				font-size: 72rpx;
				font-family: PingFang SC-Semibold, PingFang SC;
				font-weight: 600;
				color: #FFFFFF;
				line-height: 132rpx;
				text-align: center;
				margin: 0 auto;
			}
		}
	}
</style>